var boo = false;
function checkImg(inputVal) {
    //java写一个验证图形验证码的方法
    //验证失败时，需要更换图片验证码
    //验证通过返回true，否则返回false
    var $captcha = $("#captcha");
    var inputVal =  $captcha.val();
    $.ajax({
        async: false,
        type: 'POST',
        url: '${base}/varifyCode.html',
        data: {
            captcha: $('.captcha').val()
        },
        dataType: 'json',
        success: function (data) {
            if (data) {
                boo = data;
                boo = true;
            }
            else {
                boo = data;
                $(".captchaImage").attr("src", "${base}/common/captcha.html?captchaId");
                var errText =inputVal.length == 0 ? "请输入图形验证码" : "图形验证码不正确";
                $.message("warn", errText);
                boo = false;
            }
        }
    });
    return boo;
}

$(function(){
    $(".loginButton").click(function(){
        if(boo){
            document.getElementById('loginForm').submit();
        }
        else{
            return;
        }
    })
});

msg = "";
$().ready(function () {

    var $loginForm = $("#loginForm");
    var $enPassword = $("#enPassword");
    var $username = $("#username");
    var $password = $("#password");
    var $captcha = $("#captcha");
    var $captchaImage = $("#captchaImage");
    var $isRememberUsername = $("#isRememberUsername");
    // 记住用户名
    if (getCookie("adminUsername") != null) {
        $isRememberUsername.prop("checked", true);
        $username.val(getCookie("adminUsername"));
        $password.focus();
    } else {
        $isRememberUsername.prop("checked", false);
        $username.focus();
    }

    // 更换验证码
    $captchaImage.click(function () {
        $captchaImage.attr("src", "${base}/common/captcha.html?captchaId=${captchaId}&timestamp=" + (new Date()).valueOf());
    });
    // 表单验证、记住用户名
    $loginForm.submit(function () {
        if ($username.val() == "") {
            $.message("warn", "请输入您的用户名！");
            return false;
        }
        if ($password.val() == "") {
            $.message("warn", "请输入登录密码！");
            return false;
        }

        if ($captcha.val() == "") {
            $.message("warn", "请输入图形验证码！");
            return false;
        }

        if ($isRememberUsername.prop("checked")) {
            addCookie("adminUsername", $username.val(), {expires: 7 * 24 * 60 * 60});
        } else {
            removeCookie("adminUsername");
        }

        var rsaKey = new RSAKey();
        rsaKey.setPublic(b64tohex("${modulus}"), b64tohex("${exponent}"));
        var enPassword = hex2b64(rsaKey.encrypt($password.val()));
        $enPassword.val(enPassword);

    });
    if (msg != "") {
        $.message("warn", msg);
    }
});